/**
 * Project  : Looker
 * Package  : net.freestu.looker.monitor
 * File     : ContentParser.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : Aug 27, 2009
 * Modified : Aug 27, 2009
 * License  : Apache License 2.0 
 */
package net.freestu.looker.monitor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.freestu.looker.common.Category;
import net.freestu.looker.common.NewsEntity;


/**
 *  新闻正文解析
 *  解析的结果是一个新闻详细对象
 */
public class ContentParser extends AbstractParser
{
	/**
	 * 新闻对象
	 */
	private NewsEntity newsEntity;
	
	
	
	/**
	 * 构造函数
     * @param html
     * @param siteConfiguration
     */
    public ContentParser(String html, Category category)
    {
	    super(html, category);
	    this.newsEntity = new NewsEntity();
    }
    
    /**
     * 以新闻对象为构造函数
     * @param newsEntity
     */
    public ContentParser(NewsEntity newsEntity)
    {
	    super(newsEntity.getHtml(), newsEntity.getCategory());
	    this.newsEntity = newsEntity;
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.monitor.AbstractParser#getParsed()
     */
    @Override
    public Object getParsed()
    {
	    return this.newsEntity;
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.monitor.AbstractParser#parse()
     */
    @Override
    public void parse()
    {
    	newsEntity.setCategory(category);										//新闻栏目
		newsEntity.setTitle(parseField("view.indexpatterns.title"));			//新闻标题
		newsEntity.setAuthor( parseField("view.indexpatterns.author"));			//新闻作者
		newsEntity.setContent( parseField("view.indexpatterns.content"));		//新闻正文				
		newsEntity.setDate(parseDate(parseField("view.indexpatterns.date")));	//新闻发布时间
		
	}

	
	/**
	 * 分析新闻内容
	 */
	private String parseField(String name)
	{
		Pattern pt = Pattern.compile(siteConfiguration.getString(name));
		Matcher mc = pt.matcher(html);
		return mc.find() ? mc.group(1) : "未知";
	}
	
	/**
	 * 解析日期
	 */
	private Date parseDate(String dateStr)
	{
		String format = siteConfiguration.getString("view.dateFormat");
		if(format==null || dateStr==null)
			return new Date();
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			date = new Date();
		}
		
		//处理时间补充信息
		String addition = siteConfiguration.getString("view.dateAddition");
		Calendar calc = Calendar.getInstance();
		calc.setTime(date);
		Calendar curCalc = Calendar.getInstance();
		if(addition!=null){
			//补充年
			if(addition.indexOf('y')!=-1)
				calc.set(Calendar.YEAR, curCalc.get(Calendar.YEAR));
			//补充月
			if(addition.indexOf('M')!=-1)
				calc.set(Calendar.MONTH, curCalc.get(Calendar.MONTH));
			//补充日
			if(addition.indexOf('d')!=-1)
				calc.set(Calendar.DAY_OF_MONTH, curCalc.get(Calendar.DAY_OF_MONTH));
			//补充小时
			if(addition.indexOf('h')!=-1)
				calc.set(Calendar.HOUR_OF_DAY, curCalc.get(Calendar.HOUR_OF_DAY));
			//补充分钟
			if(addition.indexOf('m')!=-1)
				calc.set(Calendar.MINUTE, curCalc.get(Calendar.MINUTE));
		}
		
		return new Date(calc.getTimeInMillis());
	}

}
